package com.lagou.controller;


import com.lagou.pojo.Resume;
import com.lagou.service.ResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/resume")
public class ResumeController {

    @Autowired
    private ResumeService resumeService;

    // http://localhost:8080/resume/findById?id=1
    @RequestMapping(value = "/findById", method = RequestMethod.GET)
    @ResponseBody
    public Resume findByid(Long id) {
        return resumeService.findById(id);
    }


    @RequestMapping(value="/listpage")
    public ModelAndView list(HttpServletRequest req, HttpServletResponse resp) {
        List<Resume> list = resumeService.list();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("list", list);
        modelAndView.setViewName("index");
        return modelAndView;
    }

    @RequestMapping(value = "/deleteById")
    public String deleteById(HttpServletRequest res,
                             HttpServletResponse resp,
                             Long id) {
        resumeService.deleteById(id);
        return "redirect:/resume/listpage";
    }

    @RequestMapping(value = "add")
    public String add(HttpServletRequest res,
                      HttpServletResponse resp,
                      Resume resume) {
        resumeService.save(resume);
        return "redirect:/resume/listpage";
    }

    @RequestMapping(value = "update")
    public String update(HttpServletRequest res,
                         HttpServletResponse resp,
                         Resume resume) {
        if(resume.getId()!=null) {
            resumeService.save(resume);
        }
        return "redirect:/resume/listpage";
    }

    @RequestMapping(value = "updatepage")
    public ModelAndView updatepage(Long id ) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("id", id);
        if(id!=null) {
            Resume resume = resumeService.findById(id);
            modelAndView.addObject("resume", resume);
        }
        modelAndView.setViewName("update");
        return modelAndView;
    }
}
